<?php

class Admin_ArticuloController extends Zend_Controller_Action
{

    private $_articuloModelo;
    private $_settings;

    public function init()
    {

        $this->_settings = Zend_Registry::get('settings');

        $this->_helper->layout->setLayout('admin');

        $this->view->headLink()->appendStylesheet('/js/elrte/css/elrte.full.css');
        $this->view->headLink()->appendStylesheet('/js/elrte/js/ui-themes/smoothness/jquery-ui-1.8.5.custom.css');
        $this->view->headLink()->appendStylesheet('/js/elfinder/css/elfinder.css');

        $this->view->headLink()->appendStylesheet('/css/layout.css');
        $this->view->headScript()->appendFile('/js/jquery-1.3.2.min.js');
        $this->view->headScript()->appendFile('/js/easyTooltip.js');
        $this->view->headScript()->appendFile('/js/jquery-ui-1.7.2.custom.min.js');

        $this->view->headScript()->appendFile('/js/elrte/js/elrte.min.js');
        $this->view->headScript()->appendFile('/js/elrte/js/i18n/elrte.es.js');

        $this->view->headScript()->appendFile('/js/elfinder/js/elfinder.min.js');
        $this->view->headScript()->appendFile('/js/elfinder/js/i18n/elfinder.es.js');

        $this->view->headScript()->appendFile('/js/hoverIntent.js');
        $this->view->headScript()->appendFile('/js/superfish.js');

        $this->view->headScript()->appendFile('/js/jquery.alerts.js');
        $this->view->headLink()->appendStylesheet('/css/jquery.alerts.css');

        $this->view->headScript()->appendFile('/js/jquery.filestyle.mini.js');
        $this->view->headScript()->appendFile('/js/custom.js');

        $this->_articuloModelo = new admin_Model_ArticuloMapper();
        
    }

    public function preDispatch()
    {
        if (admin_Model_Login::isLoggedIn()) {
                    $this->view->loggedIn = true;
                    $this->_administrador = admin_Model_Login::getIdentity();
                } else {
                    $this->_redirect('/admin/login/');
                }
    }

    public function indexAction()
    {
             
        $pagina = $this->_getParam('pagina', 1);
        $itemsPorPagina = $this->_settings->admin->pag->itemsPagina;
	$rangoPaginado  = $this->_settings->admin->pag->rangoPagina;

        $articulos = $this->_articuloModelo->obtenerTodos();
        
        $paginator = Zend_Paginator::factory($articulos);
        $paginator->setItemCountPerPage($itemsPorPagina)
                  ->setCurrentPageNumber($pagina)
	          ->setPageRange($rangoPaginado);

        Zend_Paginator::setDefaultScrollingStyle('Sliding');
	Zend_View_Helper_PaginationControl::setDefaultViewPartial('control_paginado.phtml');
        
        $this->view->headTitle('Listado de Articulos');
        $this->view->titulo = 'Listado de Articulos';

        $this->view->paginator = $paginator;
        $this->view->articulos = $paginator;

        $this->view->administrador = $this->_administrador;

    }

    public function crearAction()
    {
        $form = new admin_Form_Articulo();
                $form->setAction('/admin/articulo/agregar/');
                $this->view->form = $form;
                $this->view->headTitle('Crear Articulo');
                $this->view->titulo == 'Crear Articulo';
                $this->view->administrador =  $this->_administrador;
    }

    public function agregarAction()
    {
                $this->view->headTitle('Agregar Articulo');
                $this->view->titulo = 'Agregar Articulo';
                $this->view->administrador = $this->_administrador;
        
                $form = new admin_Form_Articulo();
                $form->setAction('/admin/articulo/agregar/'); // $this->_helper->url(array('agregar','usuario','admin'))
        
                if ($this->getRequest()->isPost()) {
        
                    $params = $this->_request->getPost();
        
                    if (!$form->isValid($params)) {
        
                        $form->populate($params);
                        $this->view->form = $form;
                        return $this->render('crear');

                    }

                    $articulo = new admin_Model_Articulo();
                    $fecha   = new Zend_Date();

                    $articulo->setCatId($form->cat_id->getValue());
                    $articulo->setMetaTags($form->meta_tags->getValue());
                    $articulo->setMetaDescripcion($form->meta_descripcion->getValue());
                    $articulo->setTitulo($form->titulo->getValue());
                    $articulo->setH1($form->h1->getValue());
                    $articulo->setContenido($form->contenido->getValue());
                    $articulo->setDescripcion($form->descripcion->getValue());
                    $articulo->setTags($form->tags->getValue());
                    $articulo->setSlug($form->titulo->getValue());
                    $articulo->setFecha($fecha->get(Zend_Date::ISO_8601));

                    if ($form->miniatura->isUploaded()) {

                        $nombreOriginal = pathinfo($form->miniatura->getFileName());
                        $nombreNuevo = $nombreOriginal['dirname'] . '/' . md5(time()) . '.' . $nombreOriginal['extension'];

                        $form->miniatura->addFilter('Rename', array('target' => $nombreNuevo, 'overwrite' => true));
                        $form->miniatura->receive();

                        $articulo->setMiniatura($form->miniatura->getFileName());

                    } else {

                        $articulo->setMiniatura('default.jpg');

                    }
        
                    $this->_articuloModelo->agregar($articulo);
                    $this->_redirect('/admin/articulo/');
                }
        
                $this->view->form = $form;
                $this->render('crear');
    }

    public function editarAction()
    {
        $id   = (int) $this->_request->getParam('id',0);
        $form = new admin_Form_Articulo();
        $form->setAction('/admin/articulo/editar/id/'.$id);
        $this->view->headTitle('Editar Articulo');
        $this->view->titulo = 'Editar Articulo';
        $this->view->administrador = $this->_administrador;
        $data = $this->_articuloModelo->obtenerPorId($id);

        if($this->getRequest()->isPost()){

             $params = $this->_request->getPost();

             if(!$form->isValid($params)){

                 $form->populate($params);
                 $this->view->form = $form;
                 return $this->render('crear');

             }

            $articulo = new admin_Model_Articulo();
            $articulo->setCatId($form->cat_id->getValue());
            $articulo->setMetaTags($form->meta_tags->getValue());
            $articulo->setMetaDescripcion($form->meta_descripcion->getValue());
            $articulo->setTitulo($form->titulo->getValue());
            $articulo->setH1($form->h1->getValue());
            $articulo->setContenido($form->contenido->getValue());
            $articulo->setDescripcion($form->descripcion->getValue());
            $articulo->setTags($form->tags->getValue());
            $articulo->setSlug($form->titulo->getValue());
            $articulo->setId($id);

            if ($form->miniatura->isUploaded()) {

                if (file_exists($data->miniatura)) {
                    @unlink($data->miniatura);
                }

                $nombreOriginal = pathinfo($form->miniatura->getFileName());
                $nombreNuevo = $nombreOriginal['dirname'] . '/' . md5(time()) . '.' . $nombreOriginal['extension'];

                $form->miniatura->addFilter('Rename', array('target' => $nombreNuevo, 'overwrite' => true));
                $form->miniatura->receive();

                $articulo->setMiniatura($form->miniatura->getFileName());

            } else {

                $articulo->setMiniatura($data->miniatura);

            }


            $this->_articuloModelo->editar($articulo);
            $this->_redirect('/admin/articulo/editar/id/'.$id.'/');

        }else{

            if($data === null){
                $this->_redirect('/admin/articulo/');
            }

            $form->populate($data->toArray());
            $this->view->form = $form;
            $this->view->miniatura = $data->miniatura;

            $this->render('crear');

        }

    }

    public function eliminarAction()
    {
        $id   = (int) $this->_request->getParam('id',0);
        $this->_articuloModelo->eliminar($id);
        $this->_redirect('/admin/articulo/');
    }


}